package com.hinner.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.hinner.common.JwtTokenUtils;
import com.hinner.common.Result;
import com.hinner.entity.Company;
import com.hinner.entity.User;
import com.hinner.entity.vo.ModifyPasswordVO;
import com.hinner.entity.vo.QueryCompanyVO;
import com.hinner.entity.vo.QueryUserVO;
import com.hinner.entity.vo.UserVO;
import com.hinner.service.CompanyService;
import com.hinner.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/company")
public class CompanyController {
    private static final Logger log = LoggerFactory.getLogger(CompanyController.class);

    @Autowired
    private CompanyService companyService;


    /**
     * 增
     * @param company
     * @return
     */
    @PostMapping("/add")
    public Result addCompany(@RequestBody Company company) {
        Company one = companyService.getOne(new LambdaQueryWrapper<Company>()
                .eq(Company::getCompanyName, company.getCompanyName()));
        if (one != null) {
            return Result.error("公司名称已存在");
        }
        return Result.success(companyService.save(company));
    }

    /**
     * 删
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result deleteCompany(@RequestParam Integer id) {
        return companyService.removeById(id) ? Result.success("删除成功") : Result.error("删除失败");
    }


    /**
     * 改
     * @param company
     * @return
     */
    @PutMapping("/update")
    public Result updateCompany(@RequestBody Company company) {
        return Result.success(companyService.updateById(company));
    }


    /**
     * 条件分页查询所有用户信息
     * @param queryParams
     * @return
     */
    @PostMapping("/list")
    public Result findAllCompany(@RequestBody QueryCompanyVO queryParams) {
        PageInfo<Company> companies = companyService.findAllCompanies(queryParams);
        return Result.success(companies);
    }


    /**
     * 查询所有公司名称，不要分页查询
     * @return
     */
    @PostMapping("/listAll")
    public Result getAllCompany() {
        List<Company> list = companyService.list();

        if (CollectionUtils.isEmpty(list)) {
            return Result.error("暂无公司");
        }
        List<Map<String, Object>> listMap = new ArrayList<>();
        for (Company company : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("companyId",company.getId());
            map.put("companyName",company.getCompanyName());
            listMap.add(map);
        }
        System.out.println("listMap = " + listMap);
        return Result.success(listMap);
    }



}
